var Amqp = require('../amqp').Amqp;
/**
 * Work with distributed transactions.
 *
 * Distributed transactions provide so-called "2-phase commit". The AMQP distributed transaction
 * model supports the X-Open XA architecture and other distributed transaction implementations.
 * The Dtx class assumes that the server has a private communications channel (not AMQP) to a
 * distributed transaction coordinator.
 *
 * @class
 * @extends events.EventEmitter
 * @exports Dtx as impl.Dtx
 * @param handler
 */
function Dtx(/**handlers.Channel*/handler){
   var dtx = this;
   this.handler = handler;
   /**
    * Confirm transaction mode.
    *
    * This method confirms to the client that the channel was successfully set to use distributed
    * transactions.
    *
    * @name Dtx#select
    * @event
    */
   handler.addListener('Amqp.Dtx.SelectOk', function(){
       dtx.emit('select');
   });
   /**
    * Confirm the start of a new distributed transaction.
    *
    * This method confirms to the client that the transaction started. Note that if a start fails,
    * the server raises a channel exception.
    *
    * @name Dtx#start
    * @event
    */
   handler.addListener('Amqp.Dtx.StartOk', function(){
       dtx.emit('start');
   });
}
require('sys').inherits(Dtx, require('events').EventEmitter);
/** Select standard transaction mode.
 *
 * This method sets the channel to use distributed transactions. The client must use this method at
 * least once on a channel before using the Start method. */
Dtx.prototype.select = function(){
    this.handler.call(Amqp.Class.Dtx, Amqp.Class.Dtx.Select);
};
/**
 * Start a new distributed transaction.
 *
 * This method starts a new distributed transaction. This must be the first method on a new channel
 * that uses the distributed transaction mode, before any methods that publish or consume messages.
 *
 * @param {Object} args
 * @param {String} args.dtxIdentifier The distributed transaction key. This identifies the
 *      transaction so that the AMQP server can coordinate with the distributed transaction
 *      coordinator. */
Dtx.prototype.start = function(args){
    this.handler.call(Amqp.Class.Dtx, Amqp.Class.Dtx.Start, args);
};
exports.Dtx = Dtx;
